<?php
defined('IN_IA') or exit('Access Denied');
require_once IA_ROOT . '/addons/xn_erp/inc/function.php';
global $_GPC, $_W;
$op = $_GPC['op'] ? "&op=" . $_GPC['op'] : '';
$url = $this->createWebUrl('contact') . $op;

if ($_GPC['op'] == 'list') {

    if ($_GPC['api'] == 'list') {
        $page = $_GPC['page'] ? $_GPC['page'] : 0;
        $limit = $_GPC['limit'] ? $_GPC['limit'] : 10;
        $contact = pdo_getslice('xn_erp_contact', array(), array($page, $limit), $count, array(), "", "id DESC");

        $contact_data = array("code" => 0, "msg" => "", "count" => $count, "data" => $contact);
        $contact_json = json_encode($contact_data);
        echo $contact_json;
        exit;
    }
    include $this->template('web/contact/contact_list');
}

if ($_GPC['op'] == 'add') {

    if ($_GPC['api'] == 'add') {
        $unit_name = pdo_getcolumn('xn_erp_unit', array('id' => $_GPC['unit_id']), 'name', 1);
        $category_name = pdo_getcolumn('xn_erp_category', array('id' => $_GPC['category_id']), 'name', 1);
        $contact_data = array(
            'name' => $_GPC['name'],
            'number' => $_GPC['number'],
            'category_id' => $_GPC['category_id'],
            'category_name' => $category_name,
            'link' => $_GPC['link'],
            'leveid' => 1,
            'levename' => '金牌客户',
            'logistics_code' => $_GPC['logistics_code'],
            'default_storage' => $_GPC['default_storage'],
            'saleman' => $_GPC['saleman'],
            'zipcode' => $_GPC['zipcode'],
            'fax' => $_GPC['fax'],
            'address' => $_GPC['address'],
            'area' => $_GPC['area'],
            'remark' => $_GPC['remark'],
            'disable' => 0,
            'datetime' => TIMESTAMP,
        );
        // var_dump($contact_data['status']);exit;
        $result = pdo_insert('ims_xn_erp_contact', $contact_data);
        if (!empty($result)) {
            echo "添加成功" . $unit_name;
        } else {
            echo "添加失败";
        }
        exit;
    }


    include $this->template('web/contact/contact_add');
}